package com.jcnet.bizaia.adapter;

import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jcnet.bizaia.App;
import com.jcnet.bizaia.R;
import com.jcnet.bizaia.UserEditPasswordActivity;
import com.jcnet.bizaia.bean.FavouriteVodBean;
import com.jcnet.bizaia.bean.FavouriteVoiceBean;
import com.jcnet.bizaia.bean.MainInfoBean;
import com.jcnet.bizaia.bean.StateBean;
import com.jcnet.bizaia.tools.JCFacouriteTools;
import com.jcnet.bizaia.tools.JCTools;
import com.jcnet.bizaia.tools.Toolkit;
import com.jcnet.bizaia.view.ProgressHUD;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.squareup.picasso.Picasso;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class FavouriteVoiceAdapter extends BaseAdapter {

	private Context context;
	private List<FavouriteVoiceBean> infoBeans;
	public int count;
	
	public boolean isEdit;
	
	private BroadcastReceiver broadcastReceiver;
	
	public static final String DELVOICE_SUCCESS = "DELVOICE_SUCCESS";
	
	private int markPos ;
	
	
	public FavouriteVoiceAdapter(Context context, final List<FavouriteVoiceBean> infoBeans) {
		super();
		this.context = context;
		this.infoBeans = infoBeans;
		
		broadcastReceiver = new BroadcastReceiver() {
			
			@Override
			public void onReceive(Context context, Intent intent) {
				// TODO Auto-generated method stub
				infoBeans.remove(markPos);
				count = infoBeans.size();
				notifyDataSetChanged();
			}
		};
		
		context.registerReceiver(broadcastReceiver, new IntentFilter(DELVOICE_SUCCESS));
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		count = infoBeans.size();
		return count;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		 ViewHolder holder;
        if(convertView == null)
        {
            holder = new ViewHolder();
            convertView = View.inflate(context,R.layout.comm_favourite_info, null);
            holder.imageView = (ImageView)convertView.findViewById(R.id.imageView) ;
            holder.titleTextView = (TextView)convertView.findViewById(R.id.title);
            holder.subTitleTextView = (TextView)convertView.findViewById(R.id.subTitle);
            holder.imgIconImageView = (ImageView)convertView.findViewById(R.id.icon);
            holder.layout = (LinearLayout)convertView.findViewById(R.id.itemLin);
            
            convertView.setTag(holder);
        }else
        {
            holder = (ViewHolder)convertView.getTag();
            
        }
        if(isEdit){
        	holder.imgIconImageView.setImageResource(R.drawable.del);
        }else{
        	holder.imgIconImageView.setImageResource(R.drawable.common_icon_arrow);
        	holder.layout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        }
        final FavouriteVoiceBean infoBean  =   infoBeans.get(position);
        holder.imgIconImageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				if(Toolkit.isAvaliable(context)){
					markPos = position;
					new JCFacouriteTools().delFavourite(context, infoBean.getSid(), 2);
				}else{
					Toast.makeText(context, context.getString(R.string.noNetWork), 0).show();
				}
				
			}
		});
        
       
        if(infoBean!=null){
        	
        	holder.titleTextView.setText(infoBean.getTitle());
        	holder.subTitleTextView.setText(infoBean.getDateline()+"\n\r"+infoBean.getContent());
           // aQuery.id(holder.imageView).image(App.HTTP_PATH+infoBean.getPictureurl(), options);
        	Picasso.with(context)
        	  .load(App.HTTP_PATH+infoBean.getAcsimages())
        	  .resize(80, 60)
        	  .centerCrop()
        	  .into(holder.imageView);
        }
        
		return convertView;
	}

	static class ViewHolder{
		ImageView imageView;
		TextView subTitleTextView;
		TextView titleTextView;
		ImageView imgIconImageView;
		LinearLayout layout;
	}
}
